package com.ruoyi.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.DkdConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.manager.domain.Channel;
import com.ruoyi.manager.domain.Node;
import com.ruoyi.manager.domain.VmType;
import com.ruoyi.manager.service.IChannelService;
import com.ruoyi.manager.service.INodeService;
import com.ruoyi.manager.service.IVmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.mapper.VendingMachineMapper;
import com.ruoyi.manager.domain.VendingMachine;
import com.ruoyi.manager.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-19
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private IVmTypeService vmTypeService;

    @Autowired
    private INodeService nodeService;

    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine) {
        // 1.新增设备
        // 1-1 生成8位的唯一标识,补充货道编号
        String innoCode = String.valueOf(UUID.randomUUID());
        vendingMachine.setInnerCode(innoCode);
        // 1-2 设置售货机类型表,补充设备类型表;
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        // 1-3 查询电表,补充:区域,点位,合作商等信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine, "id");
        vendingMachine.setAddr(node.getAddress()); // 设备地址
        // 1-4 设备状态
        vendingMachine.setVmStatus(DkdConstants.VM_STATUS_NODEPLAY);  // 0-未激活

        vendingMachine.setCreateTime(DateUtils.getNowDate()); // 创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate()); // 更新时间
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        // 2.新增货道
        // 2-1 声明货道集合
        List<Channel> channelList = new ArrayList<>();
        // 2-2 双层for 循环,生成货道编号
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                Channel channel = new Channel();
                channel.setChannelId(i + "-" + j); // 货道编号
                channel.setVmId(vendingMachine.getId()); // 设备ID
                channel.setInnerCode(vendingMachine.getInnerCode()); // 货道内部编码
                channel.setMaxCapacity(vmType.getChannelMaxCapacity()); // 货道最大容量
                channel.setCreateTime(DateUtils.getNowDate()); // 创建时间
                channel.setUpdateTime(DateUtils.getNowDate()); // 更新时间
                //channelService.insertChannel(channel);
                channelList.add(channel);
            }
        }
        // 2-3 批量新增货道
        channelService.insertBatchChannel(channelList);
        return result;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        // 查询点位表,补充区域,点位,合作商等信息
        if (vendingMachine.getNodeId() != null){
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node, vendingMachine, "id"); // 补充点位信息
            vendingMachine.setAddr(node.getAddress()); // 设备地址
        }

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
